<!DOCTYPE HTML>
<!--generated with sswg-->
<html lang="en">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<head>
    <title>introduction_tutorial</title>
    <link rel="stylesheet" href="sswg.css">
    <link rel="stylesheet" href="style.css">
    <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<div style="max-width:1200px; margin:auto;">
<div style="text-align:left;">
<h1 id="Introduction Tutorial">
Introduction Tutorial
</h1><br>
<h2 id="Creating and running a game">
Creating and running a game
</h2><br>
1) Open your preferred text editor. If you're new to programming, here are some suggestions:<br>
&nbsp;&nbsp;&nbsp;&nbsp;* Atom<br>
&nbsp;&nbsp;&nbsp;&nbsp;* Visual Studio Code<br>
&nbsp;&nbsp;&nbsp;&nbsp;* Sublime Text<br>
<br>
<br>
<h3 id="2) Create a new project">
2) Create a new project
</h3>Create a new folder for your project. This will contain scripts and assets.<br>
For ursina to find 3d models and textures, they should be in the same folder or in<br>
a folder below the starting script.<br>
<br>
In this folder, create a new file called main.py. This will contain our little game.<br>
<br>
<br>
<h3 id="3) Import ursina">
3) Import ursina
</h3>At the top of the file, write:<br>
<code_block id="code_block_0"><button class="copy_code_button" onclick="copy_to_clipboard(code_block_0)">copy</button><purple>from</purple> ursina <purple>import</purple> *
</code_block>
<br>
This will import all the necessary things we need from the engine in one line.<br>
It means something like 'from ursina import all'. It's also possible to import parts of the engine<br>
like this:<br>
<code_block id="code_block_1"><button class="copy_code_button" onclick="copy_to_clipboard(code_block_1)">copy</button><purple>from</purple> ursina.prefabs <purple>import</purple> Button
</code_block>
<br>
Or import something that's not included by default:<br>
<code_block id="code_block_2"><button class="copy_code_button" onclick="copy_to_clipboard(code_block_2)">copy</button><purple>from</purple> ursina.prefabs.first_person_controller <purple>import</purple> FirstPersonController
</code_block>
<br>
<br>
<h3 id="4) Example code">
4) Example code
</h3>Here's a really basic example game with comments to explain what we're doing.<br>
We will simply make an orange box move right and left when we press 'd' or 'a'.<br>
<br>
<code_block id="code_block_3"><button class="copy_code_button" onclick="copy_to_clipboard(code_block_3)">copy</button><purple>from</purple> ursina <purple>import</purple> *

<gray># create a window</gray>
app = Ursina()

<gray># most things in ursina are Entities. An <olive>Entity</olive> is a thing you place in the world.</gray>
<gray># you can think of them as GameObjects in Unity or Actors in Unreal.</gray>
<gray># the first parameter tells us the <olive>Entity</olive><green>'s model will be a <yellow>3</yellow>d-model called '</green>cube'.</gray>
<gray># ursina includes some basic models like <green>'cube'</green>, <green>'sphere'</green> and <green>'quad'</green>.</gray>

<gray># the next parameter tells us the model's color should be orange.</gray>

<gray># <green>'scale_<olive>y</olive>=<yellow>2</yellow>'</green> tells us how big the entity should be in the vertical axis, how tall it should be.</gray>
<gray># in ursina, positive x is right, positive y is up, and positive z is forward.</gray>

player = <olive>Entity</olive>(<olive>model</olive>=<green>'cube'</green>, <olive>color</olive>=color.orange, scale_<olive>y</olive>=<yellow>2</yellow>)

<gray># create a function called <green>'update'</green>.</gray>
<gray># this will automatically get called by the engine every frame.</gray>

<purple>def</purple> update():
&nbsp;&nbsp;&nbsp;&nbsp;player.x += held_keys[<green>'d'</green>] * time.dt
&nbsp;&nbsp;&nbsp;&nbsp;player.x -= held_keys[<green>'a'</green>] * time.dt

<gray># this part will make the player move left or right based on our input.</gray>
<gray># to check which keys are held down, we can check the held_keys dictionary.</gray>
<gray># <yellow>0</yellow> means <purple>not</purple> pressed and <yellow>1</yellow> means pressed.</gray>
<gray># time.dt is simply the time since the last frame. by multiplying with this, the</gray>
<gray># player will move at the same speed regardless of how fast the game runs.</gray>


<purple>def</purple> input(key):
&nbsp;&nbsp;&nbsp;&nbsp;<purple>if</purple> key == <green>'space'</green>:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;player.y += <yellow>1</yellow>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;invoke(setattr, player, <green>'y'</green>, player.y-<yellow>1</yellow>, dela<olive>y</olive>=.<yellow>2</yellow><yellow>5</yellow>)


<gray># start running the game</gray>
app.run()


</code_block>
<br>
<h3 id="5) Running the script">
5) Running the script
</h3>Now we have to tell Python to run the script. To do this, open the command line and<br>
go to the current folder. An easy way to do this is to type 'cmd' in the address bar.<br>
Run the command below.<br>
<br>
<code_block id="code_block_4"><button class="copy_code_button" onclick="copy_to_clipboard(code_block_4)">copy</button>python main.py
</code_block>
<br>
An alternative is to install the 'atom-python-run' package in Atom so you can start<br>
the game by pressing F5.<br>
<br>
<script>
function copy_to_clipboard(containerid) {
    var range = document.createRange()
    range.selectNode(containerid)
    window.getSelection().removeAllRanges()
    window.getSelection().addRange(range)
    document.execCommand("copy")
    window.getSelection().removeAllRanges()
}
</script>
<br>
<br>
</body>
</html>